# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.su>

_basename=leptos
_realname=cargo-${_basename}
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.2.34
pkgrel=1
pkgdesc='Build tool for Leptos (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://leptos.dev/'
msys2_repository_url='https://github.com/leptos-rs/cargo-leptos'
msys2_references=(
  'archlinux: cargo-leptos'
  'purl: pkg:cargo/cargo-leptos'
)
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-binaryen"
         "${MINGW_PACKAGE_PREFIX}-cargo-generate"
         "${MINGW_PACKAGE_PREFIX}-rust-wasm")
makedepends=("${MINGW_PACKAGE_PREFIX}-lld"
             "${MINGW_PACKAGE_PREFIX}-rust"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-openssl")
source=("${msys2_repository_url}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('e431b725b425884a05c18bb4d464e7ef4cf5985dc9687101be472d3aa9ee9deb')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  "${MINGW_PREFIX}/bin/cargo.exe" fetch --target "$(rustc -vV | sed -n 's/host: //p')"
}

build() {
  cp -r "${_realname}-${pkgver}" "build-${MSYSTEM}"
  cd "build-${MSYSTEM}"

  export RUSTFLAGS="$RUSTFLAGS -C link-arg=-fuse-ld=lld"
  export OPENSSL_NO_VENDOR=1

  "${MINGW_PREFIX}/bin/cargo.exe" build \
    --release \
    --frozen
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"

  "${MINGW_PREFIX}/bin/cargo.exe" test \
    --release \
    --frozen
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  install -Dm755 "target/release/${_realname}.exe" "${pkgdir}${MINGW_PREFIX}/bin/${_realname}.exe"

  cd "${srcdir}/build-${MSYSTEM}"

  install -Dm644 "README.md" "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/README.md"
  install -Dm644 "LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
